package kr.saeildamil.project.secretary.fragment;

import kr.saeildamil.project.secretary.R;
import kr.saeildamil.project.secretary.Setup;
import kr.saeildamil.project.secretary.Today;
import kr.saeildamil.project.secretary.DB.DBHelper;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class UserAdd extends Fragment {
	EditText setupName;
	EditText setupAge;
	String date;
	String name;
	String age;
	Spinner spin;
	String occupation;
	ArrayAdapter<CharSequence>adspin;
	Button userAdd;
	DBHelper dbHelper;
	SQLiteDatabase db;
	Cursor cursor;
	Today today;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root=inflater.inflate(R.layout.useradd, container, false);

		//findId
		setupName=(EditText) root.findViewById(R.id.setup_name);
		setupAge=(EditText) root.findViewById(R.id.setup_age);
		spin=(Spinner) root.findViewById(R.id.setup_occupation);
		userAdd=(Button) root.findViewById(R.id.user_add);
		
		//spinner
		adspin=ArrayAdapter.createFromResource(getActivity(), R.array.occupation, android.R.layout.simple_spinner_item);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adspin);
		spin.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				occupation=adspin.getItem(position).toString();
				Toast.makeText(getActivity(), occupation, 0).show();
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});
		userAdd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				today = new Today();
				date=today.currentdate2;
				dbHelper=new DBHelper(getActivity());
				db=dbHelper.getWritableDatabase();
				
				name=setupName.getText().toString();
				age=setupAge.getText().toString();
				
				dbHelper=new DBHelper(getActivity());
				db = dbHelper.getWritableDatabase();
				db.execSQL("INSERT INTO user VALUES(null, '"+ date + "','"+name+"','"+ occupation +"','" + age + "','성별');");
				db.close();
				
				Toast.makeText(getActivity(), "데이터베이스 생성완료", 0).show();
				
				Intent intent=new Intent(getActivity(), Setup.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
				startActivity(intent);
			}
		});
		return root;
	}
}
